<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="接站地点" prop="checkIn">
        <el-input
          v-model="queryParams.checkIn"
          placeholder="请输入接站地点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送站地点" prop="checkOut">
        <el-input
          v-model="queryParams.checkOut"
          placeholder="请输入送站地点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option label="未上架" value="0" />
          <el-option label="已上架" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading" :data="carList" row-key="id" :expand-row-keys="[]" @selection-change="handleSelectionChange"
      @expand-change="handleExpandChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column type="expand" width="30">
        <template #default="props">
          <el-row justify="center">
            <el-table v-loading="props.row.loading" style="width:95%" :data="props.row.cars">
              <el-table-column label="车辆图片" align="center" prop="photos" width="100">
                <template #default="scope">
                  <el-image style="width: 40px;height: 40px" :src="scope.row.photos.split(',')[0]" :preview-src-list="scope.row.photos.split(',')" />
                </template>
              </el-table-column>
              <el-table-column label="车辆类型" min-width="100" align="center" prop="carType" />
              <el-table-column label="座位数" align="center" prop="carSeat" />
              <el-table-column label="车辆颜色" align="center" prop="carColor" />
              <el-table-column label="创建人" align="center" prop="createBy" />
              <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
              <el-table-column label="状态" align="center" prop="status" width="80">
                <template #default="scope">
                  {{ scope.row.status=='0'?'未上架':'已上架' }}
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button v-if="scope.row.status=='0'" type="text" @click="handleUpdateCar(scope.row)">编辑</el-button>
                  <el-button v-if="scope.row.status=='1'" type="text" @click="handleViewCar(scope.row)">查看</el-button>
                  <el-button v-if="scope.row.status=='0'" type="text" @click="handleStatusCar(scope.row)">上架</el-button>
                  <el-button v-if="scope.row.status=='1'" type="text" @click="handleStatusCar(scope.row)">下架</el-button>
                  <el-button v-if="scope.row.status=='0'" type="text" @click="handleDeleteCar(props.row,scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="接站地点" align="center" prop="checkIn" />
      <el-table-column label="送站地点" align="center" prop="checkOut" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          {{ scope.row.status=='0'?'未上架':'已上架' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="220" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleStatus(scope.row)">上架</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleStatus(scope.row)">下架</el-button>
          <el-button type="text" @click="handleAddCar(scope.row)">添加车型</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <modify-reception ref="modifyReceptionRef" @submit="getList" />
    <modify-car ref="modifyCarRef" @submit="getCarList" />
  </div>
</template>

<script setup name="Logistics_Car">
import { reactive } from '@vue/runtime-core'
import { getCurrentInstance, ref, toRefs } from 'vue'
import ModifyReception from './modify'
import ModifyCar from './modifyCar'
import _ from 'lodash'
import {
  delVehicle,
  getVehicle,
  listVehicle,
  changeStatus,
  listCar,
  changeStatusCar,
  getCar,
  delCar
} from '@/api/logex/car'


const { proxy } = getCurrentInstance()

const carList = ref([])

const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    checkIn: undefined,
    checkOut: undefined,
    status: undefined
  }
})

const { queryParams } = toRefs(data)

/** 查询接站信息列表 */
function getList() {
  loading.value = true

  listVehicle(queryParams.value).then(response => {
    carList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleExpandChange(row) {
  if (!row.cars) {
    row.loading = true
    listCar({
      vehicleId: row.id
    }).then(res => {
      row.loading = false
      row.cars = res.rows
    })
  }
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs['modifyReceptionRef'].open({
    title: '添加接送站地点'
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  proxy.$modal.loading()
  getVehicle(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyReceptionRef'].open({
      title: '修改接送站地点',
      vehicle: response.data
    })
  })
}

function handleView(row) {
  proxy.$modal.loading()
  getVehicle(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyReceptionRef'].open({
      title: '查看接送站地点',
      vehicle: response.data,
      isView: true
    })
  })
}

function handleStatus(row) {
  changeStatus({
    id: row.id,
    status: row.status == '0' ? '1' : '0'
  }).then(getList)
}

function handleStatusCar(row) {
  changeStatusCar({
    id: row.id,
    status: row.status == '0' ? '1' : '0'
  }).then(getCarList(row.vehicleId))
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm(`是否确认删除"${row.checkIn} - ${row.checkOut}"的数据项？`).then(function() {
    loading.value = true
    return delVehicle(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).finally(() => {
    loading.value = false
  })
}


function handleAddCar(row) {
  proxy.$refs['modifyCarRef'].open({
    title: '添加车型',
    vehicleId: row.id
  })
}

function handleUpdateCar(row) {
  proxy.$modal.loading()
  getCar(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyCarRef'].open({
      title: '修改车型',
      car: response.data
    })
  })
}

function handleViewCar(row) {
  proxy.$modal.loading()
  getCar(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyCarRef'].open({
      title: '查看车型',
      car: response.data,
      isView: true
    })
  })
}

function handleDeleteCar(vehicle, car) {
  proxy.$modal.confirm('是否确认删除车型"' + car.carType + '"的数据项？').then(function() {
    vehicle.loading = true
    return delCar(car.id)
  }).then(() => {
    listCar({
      vehicleId: vehicle.id
    }).then(res => {
      vehicle.cars = res.rows
    })
    proxy.$modal.msgSuccess('删除成功')
  }).finally(() => {
    vehicle.loading = false
  })
}

function getCarList(vehicleId) {
  const vehicle = _.find(carList.value, (c) => {
    return c.id == vehicleId
  })
  vehicle.loading = true
  listCar({
    vehicleId: vehicleId
  }).then(res => {
    vehicle.loading = false
    vehicle.cars = res.rows
  })
}

getList()
</script>
